package cn.com.cifi.mars.mapper.wrapper;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.com.cifi.mars.entity.ReturnPlanVersion;
import cn.com.cifi.mars.mapper.mars.ReturnPlanVersionMapper;

/**
 * @author: JingYuanXiang
 * @date: 2020/11/8 15:20
 */
@Component
public class ReturnPlanVersionWrapper {

    @Autowired
    private ReturnPlanVersionMapper returnPlanVersionMapper;

    /**
     * 查询回款计划版本数量
     * @param projectId 项目id
     * @param stateList 版本状态列表
     * @param isDelete 是否删除
     * @return
     */
    public Integer selectCountByProjectIdAndVersionState(String projectId, List<String> stateList, int isDelete){
        QueryWrapper<ReturnPlanVersion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",projectId)
                .eq("is_delete", isDelete)
                .in("version_state",stateList);
        return returnPlanVersionMapper.selectCount(queryWrapper);
    }

    /**
     * 查询回款计划版本数量
     * @param stageId 分期id
     * @param stateList 版本状态列表
     * @param isDelete 是否删除
     * @return
     */
    public Integer selectCountByStageIdAndVersionState(String stageId, List<String> stateList, int isDelete){
        QueryWrapper<ReturnPlanVersion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stage_id",stageId)
                .eq("is_delete", isDelete)
                .in("version_state",stateList);
        return returnPlanVersionMapper.selectCount(queryWrapper);
    }

    /**
     * 查询当前分期下投资考核版回款计划数量
     * @param stageId 分期id
     * @param versionType 版本类型
     * @param isAssessment 是否考核版
     * @param isDelete 是否删除
     * @return
     */
    public int selectCountByIsAssessmentAndStageId(String stageId,  String versionType, int isAssessment, int isDelete) {
        QueryWrapper<ReturnPlanVersion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stage_id",stageId)
                .eq("version_type",versionType)
                .eq("is_assessment",isAssessment)
                .eq("is_delete", isDelete);
        return returnPlanVersionMapper.selectCount(queryWrapper);
    }



    /**
     * 查询最新回款计划版本
     * @param stageId 分期id
     * @param versionType 版本类型
     * @param versionState 版本状态
     * @param isDelete 是否删除
     * @return
     */
    public ReturnPlanVersion selectLatestByStageIdAndVersionTypeAndVersionState(String stageId, String versionType, String versionIdentify, List<String> versionState, int isDelete){
        QueryWrapper<ReturnPlanVersion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stage_id",stageId)
                .eq("version_type", versionType)
                .eq(StringUtils.isNotEmpty(versionIdentify),"version_identify", versionIdentify)
                .in("version_state", versionState)
                .eq("is_delete", isDelete)
                .orderByDesc("create_time")
                .last("limit 1");
        return returnPlanVersionMapper.selectOne(queryWrapper);
    }

    /**
     * 获取回款计划版本列表
     * @param stageId 分期id
     * @param versionType 版本类型
     * @param isDelete 是否删除
     * @return
     */
    public List<ReturnPlanVersion> selectByStageIdAndVersionType(String stageId, String versionType, int isDelete) {
        QueryWrapper<ReturnPlanVersion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stage_id",stageId)
                .eq("version_type", versionType)
                .eq("is_delete", isDelete)
                .orderByDesc("create_time");
        return returnPlanVersionMapper.selectList(queryWrapper);
    }

    /**
     * 更新版本状态
     * @param versionIds 版本id列表
     * @param isDelete 是否删除
     */
    public List<ReturnPlanVersion> selectList(List<String> versionIds,int isDelete) {
        QueryWrapper<ReturnPlanVersion> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",versionIds)
                .eq("is_delete", isDelete);
       return returnPlanVersionMapper.selectList(queryWrapper);
    }


    /**
     * 获取回款计划版本列表
     * @param versionType 版本类型
     * @param yearMonth 年月
     * @param isDelete 是否删除
     * @return
     */
    public List<ReturnPlanVersion> selectByYearMonth(String versionType, String yearMonth, int isDelete) {
        QueryWrapper<ReturnPlanVersion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("version_type",versionType)
                .eq("affiliation_month", yearMonth)
                .eq("is_delete", isDelete);
        return returnPlanVersionMapper.selectList(queryWrapper);
    }


}
